package com.ylbh.modules.newinfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.pojo.newinfo.dto.AddNewInfoDTO;
import com.ylbh.common.pojo.newinfo.dto.NewInfoDetailsDTO;
import com.ylbh.common.pojo.newinfo.dto.NewInfoListDTO;
import com.ylbh.common.pojo.newinfo.vo.NewInfoDetailsVO;
import com.ylbh.common.pojo.newinfo.vo.NewInfoListVO;
import com.ylbh.common.pojo.newinfo.vo.NewsClassVO;
import com.ylbh.common.utils.*;
import com.ylbh.modules.newinfo.service.ShoppingNewsInfoService;
import com.ylbh.modules.newinfo.mapper.ShoppingNewsInfoMapper;
import com.ylbh.modules.newinfo.entity.ShoppingNewsInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 新闻内容
 * @author czx
 * @date 2019-04-11 10:31:38
 */
@Service
public class ShoppingNewsInfoServiceImpl extends ServiceImpl<ShoppingNewsInfoMapper, ShoppingNewsInfo> implements ShoppingNewsInfoService {

    @Autowired
    private ShoppingNewsInfoMapper shoppingNewsInfoMapper;

    /**
     * 新闻内容列表
     * @param newInfoListDTO
     * @return
     */
    @Override
    public PageUtils queryNewsInfoList(NewInfoListDTO newInfoListDTO) {
        Page<Object> page = new Page<>(newInfoListDTO.getPage(), newInfoListDTO.getLimit());
        List<NewInfoListVO> infoList = shoppingNewsInfoMapper.selectNewInfoList(newInfoListDTO, page);
        return new PageUtils(new Page<NewInfoListVO>().setTotal(page.getTotal()).setRecords(infoList));
    }

    /**
     * 新闻内容详情
     * @param newsId
     * @return
     */
    @Override
    public NewInfoDetailsVO getNewInfoDetailsByNewsId(String newsId) {
        NewInfoDetailsVO details = shoppingNewsInfoMapper.selectNewInfoDetailsByNewsId(newsId);
        if (null == details.getLikeNumber())
            details.setLikeNumber(0);
        if (null == details.getReadingNumber())
            details.setReadingNumber(0);
        if (null == details.getForwardingNumber())
            details.setForwardingNumber(0);
        if (null == details.getCollectionNumber())
            details.setCollectionNumber(0);
        if (null == details.getCommentsNumber())
            details.setCommentsNumber(0);
        return details;
    }

    /**
     * 修改新闻内容
     * @param newInfoDetailsDTO
     */
    @Override
    public void modifyNewInfoDetailsByNewsId(NewInfoDetailsDTO newInfoDetailsDTO) {
        if (newInfoDetailsDTO.getTitleImage().size() > 0) {
            newInfoDetailsDTO.setUrl(newInfoDetailsDTO.getTitleImage().get(0).getUrl().substring(newInfoDetailsDTO.getTitleImage().get(0).getUrl().lastIndexOf("/")+1));
        }
        if (StringUtils.isNotBlank(newInfoDetailsDTO.getNewContent())) {
            newInfoDetailsDTO.setNewContent(newInfoDetailsDTO.getNewContent().replace("<video class", "<video playsinline webkit-playsinline class"));
            newInfoDetailsDTO.setNewContent(newInfoDetailsDTO.getNewContent().replace("&tp=webp", ""));
            String url = UploadHtmlFile.uploadHtmlOss(newInfoDetailsDTO.getNewContent(), "news_info_file");
            newInfoDetailsDTO.setContent("https://file.yilianbaihui.cn/news_info_file/" + url);
        }
        shoppingNewsInfoMapper.updateNewInfoByNewsId(newInfoDetailsDTO);
    }

    /**
     * 删除新闻
     * @param list
     */
    @Override
    public void removeNewInfoByNewsIds(List<String> list) {
        shoppingNewsInfoMapper.deleteNewInfoByNewsIds(list);
    }

    /**
     * 新增新闻
     * @param addNewInfoDTO
     */
    @Override
    public void addNewsInfo(AddNewInfoDTO addNewInfoDTO) {
        IdWorker worker = new IdWorker();
        addNewInfoDTO.setNewsId(worker.nextId());
        if (addNewInfoDTO.getTitleImage().size() > 0) {
            addNewInfoDTO.setUrl(addNewInfoDTO.getTitleImage().get(0).getUrl().substring(addNewInfoDTO.getTitleImage().get(0).getUrl().lastIndexOf("/")+1));
        }
        if (StringUtils.isNotBlank(addNewInfoDTO.getNewContent())) {
            addNewInfoDTO.setNewContent(addNewInfoDTO.getNewContent().replace("<video class", "<video playsinline webkit-playsinline class"));
            addNewInfoDTO.setNewContent(addNewInfoDTO.getNewContent().replace("&tp=webp", ""));
            String url = UploadHtmlFile.uploadHtmlOss(addNewInfoDTO.getNewContent(), "news_info_file");
            addNewInfoDTO.setContent("https://file.yilianbaihui.cn/news_info_file/" + url);
            addNewInfoDTO.setContentType(0);
        }else {
            addNewInfoDTO.setContentType(1);
        }
        shoppingNewsInfoMapper.insertNewsInfo(addNewInfoDTO);
    }

    /**
     * 上传新闻图片
     * @param file
     * @return
     */
    @Override
    public String uploadNewsInfo(MultipartFile file) {
        OSSClientUtil ossClient = null;
        try{
            ossClient = new OSSClientUtil();
            String imgUrl = ossClient.uploadImg2Oss(file,"news_info");//上传文件到服务器
            if("fail".equals(imgUrl)){
                return "上传失败";
            }
            return "http://image.yilianbaihui.cn/consumer/news_info/" + imgUrl;
        }catch (Exception e) {									//捕获异常
            e.printStackTrace();
            return "上传失败";
        }
    }

    @Override
    public String uploadNewsInfoVideo(MultipartFile file, HttpServletRequest request) {
        OSSClientUtil ossClient = null;
        try {
            ossClient = new OSSClientUtil();
            String newsClassKey = request.getParameter("newsClassKey");
            String url = ossClient.uploadVideo(file, "video/"+newsClassKey);
            if("fail".equals(url)){
                return "上传失败";
            }
            return "http://image.yilianbaihui.cn/consumer/video/"+ newsClassKey + "/" + url;
        }catch (Exception e) {									//捕获异常
            e.printStackTrace();
            return "上传失败";
        }
    }

    @Override
    public List<NewsClassVO> queryClass() {
        return shoppingNewsInfoMapper.selectClass();
    }
}
